package com.newbee.adapter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.listbean.MultResultBean;
import com.newbee.sound.Sound;

public class MultChatResultListAdapter extends BaseAdapter
{

	private Context context;
	private List<MultResultBean> list;

	public MultChatResultListAdapter(Context context, List<MultResultBean> list)
	{
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount()
	{
		if (list != null)
		{
			return list.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int p)
	{
		if (list != null)
		{
			return list.get(p);
		}
		return null;
	}

	@Override
	public long getItemId(int arg0)
	{
		// TODO Auto-generated method stub
		return arg0;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(int position, View v, ViewGroup arg2)
	{
		if (v == null)
		{
			v = LayoutInflater.from(context).inflate(
					R.layout.listitem_multchat_doc, null);
		}
		ViewHolder viewHolder = new ViewHolder();
		viewHolder.head = (TextView) v.findViewById(R.id.mult_res_head);
		viewHolder.qz = (TextView) v.findViewById(R.id.mult_res_qunzu);
		viewHolder.time = (TextView) v.findViewById(R.id.mult_res_time);
		viewHolder.content = (TextView) v.findViewById(R.id.mult_res_content);

		MultResultBean bean = list.get(position);

		viewHolder.qz.setText("房间：" + bean.getQz());

		viewHolder.time.setText(bean.getTime());

		File file = new File(Sound.DIR + bean.getFile());

		if (file.exists())
		{

			try
			{
				String str = null;
				StringBuffer sb = new StringBuffer();
				BufferedReader br = new BufferedReader(new FileReader(file));
				while (null != (str = br.readLine()))
				{
					sb.append(str);
				}
				br.close();

				str = sb.toString();

				if (str.length() == 0)
				{
					viewHolder.head.setText("空");
					viewHolder.head.setTextColor(R.color.steelblue);
					viewHolder.content.setText("此群组录音会话信息为空");
				}
				else
				{
					viewHolder.head.setText("" + str.charAt(1));

					boolean ismaxLength = 80 > str.length() ? true : false;

					if (ismaxLength)
					{
						viewHolder.content.setText(str);
					}
					else
					{
						viewHolder.content
								.setText(str.subSequence(0, 70) + "…");
					}
				}

			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		else
		{
			viewHolder.head.setText("异");
			viewHolder.head.setTextColor(R.color.red);
			viewHolder.content.setText(bean.getFile());
		}

		return v;

	}

	static class ViewHolder
	{
		TextView head;
		TextView qz;
		TextView content;
		TextView time;

	}

}
